import '../index.dart';

/*数据初始化*/
class InitDataPage extends StatefulWidget {
  const InitDataPage({Key? key}) : super(key: key);

  @override
  _InitDataState createState() => _InitDataState();
}

class _InitDataState extends State<InitDataPage> {
  bool loading = true;

  @override
  void initState() {
    super.initState();
    loadCache();
  }

  Future<void> loadCache() async {
    bool loadSuccess = await DataCache.renewCacheData(context);

    if (loadSuccess) {
      Global.profile.isInitData = true;
      Global.saveProfile();
      //成功就跳转至首页
      Navigator.pushReplacementNamed(context, "start");
    } else {
      setState(() {
        loading = false;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    List<Widget> list = [];
    if (loading) {
      list.add(Padding(
        padding: EdgeInsets.all(10),
        child: CircularProgressIndicator(
          strokeWidth: 1.0,
        ),
      ));
      list.add(Text("正在初始化数据，请稍后...",
          style: Theme.of(context).textTheme.titleSmall));
    } else {
      list.add(Text("数据初始化失败，请重新启动app",
          style: Theme.of(context).textTheme.titleSmall));
    }
    // TODO: implement build
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: list,
        ),
      ),
    );
  }
}
